package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessPersonnelService;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.PersonnelDemandVO;
import com.digitalchina.lxnsy.entity.TalentTitleVO;
import com.digitalchina.lxnsy.toolkit.DateStyle;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.xml.RongCloudXML;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import chihane.jdaddressselector.BottomDialog;
import chihane.jdaddressselector.OnAddressSelectedListener;
import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;

import static com.digitalchina.lxnsy.toolkit.DateUtil.DateToString;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/9/8 10:36
 * 修改备注：
 * ================================================
 */
public class PostDemandActivity extends BaseActivity {
    @Bind(R.id.tv_post_task)
    EditText tvPostTask;
    @Bind(R.id.tv_contact_people)
    EditText tvContactPeople;
    @Bind(R.id.tv_people_num)
    EditText tvPeopleNum;
    @Bind(R.id.tv_price)
    EditText tvPrice;
    @Bind(R.id.tv_demand)
    EditText tvDemand;
    @Bind(R.id.tv_tel)
    TextView tvTel;
    @Bind(R.id.tv_address)
    EditText tvAddress;
    @Bind(R.id.tv_start_date)
    TextView tvStartDate;
    @Bind(R.id.tv_end_date)
    TextView tvEndDate;
    @Bind(R.id.post_task)
    TextView postTask;
    @Bind(R.id.demand)
    TextView demand;


    /**
     * 发布人才需求
     * id       需求或供应Id   新发布为空
     * content      任务内容
     * contactPeople 联系人
     * peopleNum    人数
     * price        价格
     * address      详细地址
     * tel          联系电话
     * startTime    开始时间
     * endTime      结束时间
     * demandCode   招聘要求编码
     * provinceCode 省份编码
     * cityCode     城市编码
     * countyCode   区县编码
     */
    private String id = "";
    private String content = "";
    private String contactPeople = "";
    private String peopleNum = "";
    private String price = "";
    private String address = "";
    private String tel = "";
    private String startTime = "";
    private String endTime = "";
    private String demandCode = "";

    private String types = "";

    // 时间选择器
    private TimePickerView pvTime;
    // 区分开始和结束时间
    private int type = -1;
    // 发布类型
    private String postType = "";

    private PersonnelDemandVO vo;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_post_demand);

    }

    @Override
    public void initMember() {

        initTimePicker();

        Map<String, Object> map = (Map<String, Object>) getIntent().getSerializableExtra("map");
        postType = map.get("postType").toString();

        if (postType.equals("editDemand")) {
            setTitle("发布人才需求");
            postTask.setText("任务内容");
            demand.setText("招聘需求");
            types = "1";
        } else {
            setTitle("发布人才供应");
            postTask.setText("服务内容");
            demand.setText("招聘需求");
            types = "0";
        }

        if (map.containsKey("data"))
            vo = (PersonnelDemandVO) map.get("data");
        if (vo != null) {
            id = vo.getId();
            content = vo.getContent();
            contactPeople = vo.getContacts();
            peopleNum = vo.getNumber() + "";
            price = vo.getPrice()+"";
            address = vo.getAddress();
            tel = vo.getTelephone();
            startTime = vo.getStartDate();
            endTime = vo.getEndDate();
            demandCode = vo.getDemand();
            // 任务名
            tvPostTask.setText(content);
            // 联系人
            tvContactPeople.setText(contactPeople);
            // 人数
            tvPeopleNum.setText(peopleNum);
            // 价格
            if (StringUtil.isStrEmpty(price) || price.equals("0")) {
                price = "0";
                tvPrice.setText("面议");
            } else
                tvPrice.setText(price);
            // 职位
            if (vo.getDemand() != null)
                tvDemand.setText(vo.getDemand());
            // 详细
            tvAddress.setText(address);
            // 电话
            tvTel.setText(tel);
            // 起始日期
            if (!StringUtil.isStrEmpty(startTime) && startTime.length() >= 10)
                tvStartDate.setText(startTime.substring(0, 10));
            else
                tvStartDate.setText(startTime);
            // 结束日期
            if (!StringUtil.isStrEmpty(endTime) && endTime.length() >= 10)
                tvEndDate.setText(endTime.substring(0, 10));
            else
                tvStartDate.setText(endTime);

        }
    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_EDIT_DEMAND_SUCCESS:// 发布成功
                        showToast("发布成功");
                        setResult(RESULT_OK);
                        finish();
                        break;
                    case MSG.MSG_EDIT_DEMAND_FIELD:// 发布失败
                        showToast(msg.obj.toString());
                        break;

                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.rl_post_task, R.id.rl_contact_people, R.id.rl_people_num, R.id.rl_price, R.id.rl_demand, R.id.rl_area, R.id.rl_address, R.id.rl_tel, R.id.rl_start_date, R.id.rl_end_date, R.id.btn_post})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_area:
                showAddressPicker();
                break;
            case R.id.rl_start_date:
                type = 0;
                pvTime.show();
                break;
            case R.id.rl_end_date:
                type = 1;
                pvTime.show();
                break;
            case R.id.btn_post:
                submit();
                break;
        }
    }

    /**
     * 提交数据
     */
    private void submit() {
        content = tvPostTask.getText().toString();
        contactPeople = tvContactPeople.getText().toString();
        peopleNum = tvPeopleNum.getText().toString();
        price = tvPrice.getText().toString();
        demandCode = tvDemand.getText().toString();
        tel = tvTel.getText().toString();
        address = tvAddress.getText().toString();
        startTime = tvStartDate.getText().toString();
        endTime = tvEndDate.getText().toString();
        if (StringUtil.isStrEmpty(content)) {
            showToast("请填写任务内容");
            return;
        }

        if (StringUtil.isStrEmpty(contactPeople)) {
            showToast("请填写联系人");
            return;
        }

        if (StringUtil.isStrEmpty(peopleNum)) {
            showToast("请填写人数");
            return;
        }

        if (StringUtil.isStrEmpty(price)) {
            showToast("请填写价格");
            return;
        }

        if (StringUtil.isStrEmpty(demandCode)) {
            showToast("请选择招聘要求");
            return;
        }

        if (StringUtil.isStrEmpty(address)) {
            showToast("请填写详细地址");
            return;
        }

        if (StringUtil.isStrEmpty(tel)) {
            showToast("请填写联系方式");
            return;
        }

        if (StringUtil.isStrEmpty(startTime)) {
            showToast("请填写开始时间");
            return;
        }

        if (StringUtil.isStrEmpty(endTime)) {
            showToast("请填写结束时间");
            return;
        }

        if (vo != null ){
            BusinessPersonnelService.editSupply(this,vo.getId(), types, content, contactPeople, peopleNum, price, address, tel, startTime, endTime, demandCode, mHandler);
        }else {
            BusinessPersonnelService.AddSupply(this, types, content, contactPeople, peopleNum, price, address, tel, startTime, endTime, demandCode, mHandler);
        }
    }


    /**
     * 所在区域选择
     */
    private void showAddressPicker() {
        final BottomDialog bottomDialog = new BottomDialog(this);
        bottomDialog.setOnAddressSelectedListener(new OnAddressSelectedListener() {
            @Override
            public void onAddressSelected(Province province, City city, County county, Street street) {
                bottomDialog.dismiss();
            }
        });
        bottomDialog.show();
    }

    /**
     * 时间选择器
     * 0=开始  1=结束
     */
    private void initTimePicker() {

        int start = 2017;
        int endDate = 2100;

        //控制时间范围,setRange方法 要在setDate 之前才有效果(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        /*Calendar calendar = Calendar.getInstance();*/

        //时间选择器
        pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                switch (type) {
                    case 0:
                        startTime = DateToString(date, DateStyle.YYYY_MM_DD);
                        tvStartDate.setText(startTime);
                        break;
                    case 1:
                        endTime = DateToString(date, DateStyle.YYYY_MM_DD);
                        tvEndDate.setText(endTime);
                        break;
                }

            }
        })
                .setType(TimePickerView.Type.YEAR_MONTH_DAY)//default is all
                .setLabel("年", "月", "日", "", "", "") //设置空字符串以隐藏单位提示   hide label
                .setRange(start, endDate)
                .build();
    }

}
